/*
 * 作者: 侯晓宇
 * 时间: 2017-01-11 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	var appInfo = require('app/AppInfo');
	var log_level = 0; //0,1,2(production)
	
	var c = {};

	function add0(m) {
		return m < 10 ? '0' + m : m
	}
	c.random = function(size){
		var m = 0;
		var n = 9;
		var r = "";
		for(var i = 0;i < size;i ++){
			r = r +parseInt(Math.random()*(n-m)+m); 
		}
		return r;
	}
	c.timeFormat = function(timestamp) {
		var date = new Date(parseFloat(timestamp));
		//2018-04-01 12:12
		return date.getFullYear() + "-" + add0(date.getMonth() + 1) + "-" + add0(date.getDate()) + " " + add0(date.getHours()) + ":" + add0(date.getMinutes());
	}
	c.afterNow = function(y,m,d,h,min){
		var t = new Date();
    	if(t.getFullYear() < y){
    		return true;
    	}else if((t.getMonth()+1) < m){
    		return true;
    	}else if(t.getDate() < d){
    		return true;
    	}else if(t.getHours() < h){
    		return true;
    	}else if(t.getMinutes() < min){
    		return true;
    	}else {
    		return false;    	
    	}
	}
	c.buildTimestamp = function(y, m, d, h, min) {
		var date = new Date(y,(m-1),d,h,min);
		var timestamp = date.getTime();
		//返回时间戳
		return timestamp;
	}

	c.gen_id = function() {
		return "" + new Date().getTime();
	}
	c.isTel = function(str) {
		var reg = /^[0-9]*\d{11}$/;
		return reg.test(str);
	},
	c.isNullStr = function(val) {
		return (val == null || typeof(val) == "undefined" || val.length == 0 || val == "null");
	}
	c.isNull = function(val) {
		return (val == null || typeof(val) == "undefined");
	};
	c.removeVirguleOfBegin = function(a) {
		while (a.substr(0, 1) == "/") {
			a = a.substr(1, a.length);
		}
		return a;
	};
	c.removeVirguleOfEnd = function(a) {
		while (a.substr(a.length - 1) == "/") {
			a = a.substr(0, a.length - 1);
		}
		return a;
	};
	c.handleUrl = function(url) {

		if (typeof(url) == 'undefined' || url == null) {
			return null;
		}
		var indexhttp = url.indexOf("http://");
		if (indexhttp != -1) { // http://xxx.sss.xxx.jpg 网络地址，直接访问
			return url;
		}
		//服务器地址的相对路径 /asset/i/ssss.jpg
		return this.removeVirguleOfEnd(appInfo.httpserverbase) + "/" + this.removeVirguleOfBegin(url);

	};

	c.debug = function(title, obj) {
		if (log_level == 0) {

			console.log(title + ":" + JSON.stringify(obj));

		} else if (log_level == 1) {

			alert(title + ":" + JSON.stringify(obj));
		}
	};
	c.l = function(obj) {
		if (log_level == 0) {

			console.log(">>>>>>>>>>>>>>>>>>>>>" + ":" + JSON.stringify(obj));

		} else if (log_level == 1) {

			alert(">>>>>>>>>>>>>>>>>>>>>" + ":" + JSON.stringify(obj));
		}
	};
	c.log = function(obj) {

		if (log_level == 0) {

			console.log(JSON.stringify(obj));

		} else if (log_level == 1) {

			alert(JSON.stringify(obj));
		}

	}

	c.remToPx = function(rem) {
		//需要提前加载zepto或者jquery
		if (this.isNull($)) {
			return 20 * rem;
		} else {
			return parseInt($("html").css("font-size").substr(0, $("html").css("font-size").length - 2)) * rem;
		}
	}


	c.pxToRem = function(px) {
		//需要提前加载zepto或者jquery
		if (this.isNull($)) {
			return px / 20;
		} else {
			return px / (parseInt($("html").css("font-size").substr(0, $("html").css("font-size").length - 2)));
		}
	}
	

	return c;
});